﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Yes.Application;
using Yes.Application.Article;
using Yes.Application.Article.Dto;
using Yes.Application.Category;
using Yes.Domain.Context;
using Yes.Domain.Entity;
using Yes.Web.Blog.Areas.Admin.Models.Article;

namespace Yes.Web.Blog.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class ArticleController : BaseController
    {
        private ICategoryService _categoryService;
        private IArticleService _articleService;
        private IUserContext _userContext;

        public ArticleController(ICategoryService categoryService, IArticleService articleService,
            IUserContext userContext)
        {
            _categoryService = categoryService;
            _articleService = articleService;
            _userContext = userContext;
        }

        public ActionResult Index()
        {
            var model = new IndexView
            {
                CategoryList = _categoryService.GetCategoryList()
            };
            return View(model);
        }

        public ActionResult Recycle()
        {
            var model = new IndexView
            {
                CategoryList = _categoryService.GetCategoryList()
            };
            return View(model);
        }

        public ActionResult Draft()
        {
            var model = new IndexView
            {
                CategoryList = _categoryService.GetCategoryList()
            };
            return View(model);
        }

        [HttpGet]
        public ActionResult Add(int id = 0)
        {
            var model = new AddView
            {
                CategoryList = _categoryService.GetCategoryList(),
                Article = _articleService.GetArticle(id) ?? new ArticleEntity
                {
                    CanComment = true
                }
            };
            return View(model);
        }


        [HttpPost]
        public ActionResult Add(ArticleAddDto dto)
        {
            var user = _userContext.CurrentUser;
            var userId = user?.Id ?? 0;
            return Json(_articleService.Add(dto, userId));
        }

        [HttpPost]
        public ActionResult Save(ArticleAddDto dto)
        {
            var user = _userContext.CurrentUser;
            var userId = user?.Id ?? 0;
            dto.Status = Domain.Enum.ArticleStatusEnum.Draft;
            return Json(_articleService.Add(dto, userId));
        }

        [HttpPost]
        public ActionResult Remove(List<int> ids)
        {
            return Json(_articleService.Remove(ids));
        }

        [HttpPost]
        public ActionResult Delete(List<int> ids)
        {
            return Json(_articleService.Delete(ids));
        }

        [HttpPost]
        public ActionResult Recovery(List<int> ids)
        {
            return Json(_articleService.Recovery(ids));
        }



        public ActionResult GetPageInfo(ArticlePagerDto dto)
        {
            return Json(new ResultDto
            {
                Status = true,
                Data = _articleService.GetPageInfo(dto)
            });
        }
    }
}